//
//  OverflowView.m
//  PagedFlowView
//
//  Created by 于 百和 on 13-4-17.
//  Copyright (c) 2013年 Taobao.com. All rights reserved.
//

#import "OverflowView.h"

@implementation OverflowView

@synthesize miniScale,dataSource,delegate,centerIndex;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        CGSize size = frame.size;
        
        int width = ImageViewSpaceLeft + ImageViewWidth;
        _pageCount = 0;
        self.clipsToBounds = YES;
        _ScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake((frame.size.width-width)/2, 0, width, size.height)];
        _ScrollView.delegate = self;
//        _ScrollView.pagingEnabled = YES;
        _ScrollView.clipsToBounds = NO;
        _ScrollView.showsHorizontalScrollIndicator = NO;
        _ScrollView.showsVerticalScrollIndicator = NO;
        [self addSubview:_ScrollView];
        
        [self addObserver:self forKeyPath:@"centerIndex" options:NSKeyValueObservingOptionNew context:nil];
    }
    return self;
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
	//实现相关操作
	id index = [change objectForKey:NSKeyValueChangeNewKey];
    if (delegate && [delegate respondsToSelector:@selector(CurrentIndexHasChange:)]) {
        [delegate CurrentIndexHasChange:index];
    }
}

-(void)toNextPage{
    [self toNextPage:1];
    
}

-(void)toNextPage:(int)i{
    
    int nextindex = centerIndex + i;
    if(nextindex >_pageCount-1)
    {
        return;
    }
    lastPoint.x +=_pageSize.width*i;
    self.centerIndex = nextindex;
    
    [self setCellFrame:YES positon:i];
    [UIView beginAnimations:@"CellAnimation" context:nil];
    [UIView setAnimationDuration:0.2];
    [_ScrollView setContentOffset:lastPoint];
    [UIView commitAnimations];
    
}

-(void)toPreviousPage{
    [self toPreviousPage:1];
}

-(void)toPreviousPage:(int)i{
    
    int preindex = centerIndex - i;
    if (preindex <0) {
        return;
    }
    lastPoint.x -=_pageSize.width*i;
    self.centerIndex = preindex;
    
    [self setCellFrame:NO positon:i];
    [UIView beginAnimations:@"CellAnimation" context:nil];
    [UIView setAnimationDuration:0.2];
    [_ScrollView setContentOffset:lastPoint];
    [UIView commitAnimations];
}

//-(void)setCellFrame:(BOOL)isNext{
//    
//    UIView *cell = [self getViewWithTag:centerIndex];// = [_cells objectAtIndex:centerIndex];
//    
//    [UIView beginAnimations:@"CellAnimation" context:nil];
//    [UIView setAnimationDuration:0.4];
//    
//    cell.frame = CGRectMake(_pageSize.width * (centerIndex + 1), ImageOffsetY, _pageSize.width +ImageViewSpaceLeft, _pageSize.height +ImageViewSpaceTop);
//    
//    if (isNext) {
//        UIView *cell = [self getViewWithTag:centerIndex-1];//[_cells objectAtIndex:centerIndex-1];
//        cell.frame = CGRectMake(_pageSize.width * (centerIndex), 0, _pageSize.width , _pageSize.height );
//        
//    }else{
//        UIView *cell = [self getViewWithTag:centerIndex+1];//[_cells objectAtIndex:centerIndex+1];
//        cell.frame = CGRectMake(_pageSize.width * (centerIndex + 2) + ImageViewSpaceLeft, 0, _pageSize.width , _pageSize.height );
//        
//    }
//
//    [UIView commitAnimations];
//    
//}

-(void)setCellFrame:(BOOL)isNext positon:(int)index{
    
    UIView *cell = [self getViewWithTag:centerIndex];// = [_cells objectAtIndex:centerIndex];
    
    [UIView beginAnimations:@"CellAnimation" context:nil];
    [UIView setAnimationDuration:0.4];
    
    cell.frame = CGRectMake(_pageSize.width * (centerIndex + 1), ImageOffsetY, _pageSize.width +ImageViewSpaceLeft, _pageSize.height +ImageViewSpaceTop);
    
    if (isNext) {
        for (int i=0; i<index; i++) {
            UIView *cell = [self getViewWithTag:centerIndex-i-1];//[_cells objectAtIndex:centerIndex-1];
            cell.frame = CGRectMake(_pageSize.width * (centerIndex-i), 0, _pageSize.width , _pageSize.height );
        }
        
        
    }else{
        for (int i=0; i<index; i++) {
            UIView *cell = [self getViewWithTag:centerIndex+i+1];//[_cells objectAtIndex:centerIndex+1];
            cell.frame = CGRectMake(_pageSize.width * (centerIndex + i+2) + ImageViewSpaceLeft, 0, _pageSize.width , _pageSize.height );
        }
        
    }
    
    [UIView commitAnimations];
    
}

-(UIView *)getViewWithTag:(NSInteger)tag{
    for (UIView *v in [_ScrollView subviews]) {
        if (v.tag == 1000+tag) {
            return  v;
        }
    }
    return nil;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    
    if (dataSource && [dataSource respondsToSelector:@selector(numberOfPagesInFlowView:)]) {
        _pageCount = [dataSource numberOfPagesInFlowView:self];
        
    }
    
    
//    self.centerIndex = (_pageCount /2)-1<0?0:(_pageCount /2)-1;
    self.centerIndex = _pageCount /2;
    
    //重置_pageSize
    if (dataSource && [dataSource respondsToSelector:@selector(sizeForPageInFlowView:)]) {
        _pageSize = [dataSource sizeForPageInFlowView:self];
    }
    
    
    for (NSInteger index=0; index<_pageCount; index++)
    {
        [self setPageAtIndex:index];
    }
        
    // 重置_scrollView的contentSize
    
    _ScrollView.contentSize = CGSizeMake(_pageSize.width *_pageCount+ImageViewSpaceLeft*3,_pageSize.height);
    CGPoint theCenter = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    _ScrollView.center = theCenter;
    
    
    lastPoint = CGPointMake(_pageSize.width *(centerIndex + 1) , 0);
    [_ScrollView setContentOffset:lastPoint];
    
}

- (void)setPageAtIndex:(NSInteger)pageIndex{
    NSParameterAssert(pageIndex >= 0);
    
    UIView *cell= [dataSource flowView:self cellForPageAtIndex:pageIndex];
    NSAssert(cell!=nil, @"datasource must not return nil");
    
    CGFloat offset = 0.0;
    if (pageIndex > centerIndex) {
        offset =ImageViewSpaceLeft;
    }
    if(pageIndex == centerIndex){
        cell.frame = CGRectMake(_pageSize.width * (pageIndex + 1)+offset, ImageOffsetY, _pageSize.width+ImageViewSpaceLeft, _pageSize.height+ImageViewSpaceTop);
    }else{
        cell.frame = CGRectMake(_pageSize.width * (pageIndex + 1)+offset, 0, _pageSize.width, _pageSize.height);
    }
    cell.tag = 1000+pageIndex;
    if (!cell.superview) {
        [_ScrollView addSubview:cell];
    }
}

-(int)GetPageDistance:(CGPoint)clickPoint
{
    int distance = 0;
    CGPoint point = [self convertPoint:clickPoint toView:_ScrollView];
    if (point.y>ImageViewHeight || point.y<0) {
        return 0;
    }
    int right = _ScrollView.contentOffset.x+_ScrollView.frame.size.width;
    int left  = _ScrollView.contentOffset.x;
    if (point.x>right) {
        distance = (point.x-right)/ImageViewWidth+1;
    }
    else if((point.x-left)<0)
    {
        distance = (point.x-left)/ImageViewWidth-1;
    }

    return distance;
}

#pragma Mark -scrollView delegate

//-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
//    
//    CGFloat Distance = scrollView.contentOffset.x - lastPoint.x;
//        
//    centerIndex += (int)Distance/_pageSize.width;
//}

-(void)dealloc
{
    [self removeObserver:self forKeyPath:@"centerIndex"];
    [super dealloc];
}

@end
